<template>
  <el-dialog title="产权退出" :visible="dialogVisible" width="60%" :before-close="handleClose">
    <el-form ref="form" :model="form" label-width="110px" label-position="left" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="产权退出日期" prop="propertyQuitDate">
            <el-date-picker clearable type="date" placeholder="选择日期" v-model="form.propertyQuitDate" size="medium" class="select"></el-date-picker>
          </el-form-item>
          <el-form-item v-show="ifBranch" label="产权登记情形" prop="registerStatus">
            <el-select v-model="form.registerStatus" size="medium" class="select" clearable>
              <el-option
                v-for="item in registerStatusOption"
                :key="item.code"
                :label="item.value"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产权退出方式" prop="propertyQuit" style="margin-left: -9px">
            <el-select v-model="form.propertyQuit" size="medium" class="select" style="width: calc(100% - 9px);margin-left: 9px" clearable>
              <el-option
                v-for="item in propertyQuitOption"
                :key="item.code"
                :label="item.value"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-show="ifBranch" label="经营状况" prop="status">
           <el-select v-model="form.status" size="medium" class="select" clearable>
              <el-option
                v-for="item in cmhkStatusOption"
                :key="item.code"
                :label="item.value"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>          
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleCancle">取 消</el-button>
      <el-button type="primary" @click="handleOut('form')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { queryALLBasicData, quitTempCompany } from '@/api/modules/propertyRight.js'
export default {
  props: {
    dialogData: {
      type: Object,
      required: true
    }
  },
  watch: {
    dialogData: {
      handler(val) {
        console.log(val, 111)
        this.dialogVisible = val.dialogVisible
        this.form.companyName = val.companyName
        this.form.cmhkId = val.cmhkId
        this.form.registerType = val.registerType
        this.form.taskId = val.taskId
      },
      deep: true
    }
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        propertyQuitDate: '',
        registerStatus: '',
        propertyQuit: '',
        status: '',
        companyName: '',
        cmhkId: '',
        registerType: '',
        taskId: ''
      },
      propertyQuitOption: [],
      cmhkStatusOption: [],
      registerStatusOption: [],
      rules: {
        // propertyQuitDate: [{ required: true, message: '请选择产权退出日期', trigger: 'change' }],
        propertyQuit: [{ required: true, message: '请选择产权退出方式', trigger: 'change' }]
      },
      ifBranch: true
    }
  },
  methods: {
    initData() {
      if (this.form.registerType === '05') {
        this.ifBranch = false
        delete this.rules.propertyQuit
      } else {
        this.ifBranch = true
      }
      queryALLBasicData({tableName: 'company_info', field: 'register_status'}).then(res => {
        this.registerStatusOption = res.data.body
      })
      queryALLBasicData({tableName: 'company_info', field: 'property_quit'}).then(res => {
        this.propertyQuitOption = res.data.body
      })
      queryALLBasicData({tableName: 'company', field: 'status'}).then(res => {
        this.cmhkStatusOption = res.data.body
      })
    },
    // 关闭弹窗
    hideDialog(isRefresh) {
      this.resetForm('form')
      this.dialogVisible = false
      this.$emit('on-close', {isClose: true, isRefresh: isRefresh})
    },
    handleClose() {
      this.hideDialog(false)
    },
    // 点击取消
    handleCancle() {
      this.hideDialog(false)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 点击确定
    handleOut(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          quitTempCompany(this.form).then(res => {
            if (res.data.code === '0') {
              this.$message.success('产权退出成功')
              this.hideDialog(true)
            } else {
              this.$message.error(res.data.message)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
  .select {
    width: 100%;
  }
</style>